package sanshui.system.trade.service;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import sanshui.system.trade.common.GlobalContain;
import sanshui.system.trade.common.TradeType;
import sanshui.system.trade.db.entity.TradeLimitOrder;
import sanshui.system.trade.db.entity.TradeLimitOrderExample;
import sanshui.system.trade.db.mapper.TradeLimitOrderMapper;
import sanshui.system.trade.model.TradeLimitOrderModel;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class TradeLimitOrderServiceImpl implements ITradeLimitOrderService{

    @Resource
    TradeLimitOrderMapper limitOrderMapper;

    @Override
    public void updateLimitOrderSelective(TradeLimitOrderModel limitOrderModel) {
        TradeLimitOrder limitOrder = BeanUtil.copyProperties(limitOrderModel, TradeLimitOrder.class);
        limitOrderMapper.updateByPrimaryKeySelective(limitOrder);
    }

    @Override
    public List<TradeLimitOrderModel> findAllLimitOrderList() {
        TradeLimitOrderExample ex = new TradeLimitOrderExample();
        TradeLimitOrderExample.Criteria criteria = ex.createCriteria();
        criteria.andPhyFlgEqualTo(TradeType.DelFlg.EXIT.getCode());

        List<TradeLimitOrder> tradeLimitOrders = limitOrderMapper.selectByExample(ex);
        return BeanUtil.copyToList(tradeLimitOrders, TradeLimitOrderModel.class);
    }

    @Override
    public List<TradeLimitOrderModel> findAllLimitOrderList(Long accId, Long workerId) {
        TradeLimitOrderExample ex = new TradeLimitOrderExample();
        TradeLimitOrderExample.Criteria criteria = ex.createCriteria();
        criteria.andPhyFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        criteria.andAccIdEqualTo(accId);
        criteria.andWorkerIdEqualTo(workerId);
        List<TradeLimitOrder> tradeLimitOrders = limitOrderMapper.selectByExample(ex);
        return BeanUtil.copyToList(tradeLimitOrders, TradeLimitOrderModel.class);
    }

    @Override
    public List<TradeLimitOrderModel> findLimitOrderListByCode(String targetCode) {
        TradeLimitOrderExample ex = new TradeLimitOrderExample();
        TradeLimitOrderExample.Criteria criteria = ex.createCriteria();
        criteria.andPhyFlgEqualTo(TradeType.DelFlg.EXIT.getCode());
        criteria.andTargetCodeEqualTo(targetCode);

        List<TradeLimitOrder> tradeLimitOrders = limitOrderMapper.selectByExample(ex);
        return BeanUtil.copyToList(tradeLimitOrders, TradeLimitOrderModel.class);
    }

    @Override
    public boolean insertLimitOrderSelective(TradeLimitOrder tradeLimitOrder) {
        return limitOrderMapper.insertSelective(tradeLimitOrder) > 0;
    }
}
